package com.dingwen.treasure.mybatisplus.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingwen.treasure.core.util.TreeUtil;
import com.dingwen.treasure.model.entity.mybatisplus.SysElement;
import com.dingwen.treasure.model.entity.mybatisplus.SysMenu;
import com.dingwen.treasure.model.entity.mybatisplus.SysUserRole;
import com.dingwen.treasure.model.vo.SysMenuVo;
import com.dingwen.treasure.mybatisplus.mapper.SysElementMapper;
import com.dingwen.treasure.mybatisplus.mapper.SysMenuMapper;
import com.dingwen.treasure.mybatisplus.mapper.SysUserRoleMapper;
import com.dingwen.treasure.mybatisplus.service.SysMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author dingwen
 * @description 针对表【sys_menu(菜单表)】的数据库操作Service实现
 * @createDate 2022-06-20 17:18:19
 */
@Service
@DS("slave")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    /**
     * sysMenuMapper
     */
    @Resource
    private SysMenuMapper sysMenuMapper;


    /**
     * sysElementMapper
     */
    @Resource
    private SysElementMapper sysElementMapper;

    /**
     * sysUserRoleMapper
     */
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 按用户id获取系统菜单
     *
     * @param userId 用户id
     * @return {@link List}<{@link SysMenu}>
     */
    @Override
    public List<SysMenu> getSysMenusByUserId(Long userId) {
        return sysMenuMapper.getSysMenusByUserId(userId);
    }

    /**
     * 获取树系统菜单
     *
     * @param userId 用户id
     * @return {@link List}<{@link SysMenuVo}>
     */
    @Override
    public List<SysMenuVo> getSysMenusForTree(Long userId) {

        // 获取用户菜单
        List<SysMenu> menus = sysMenuMapper.getSysMenusByUserId(userId);

        List<Long> menuIds = menus.parallelStream()
                .map(SysMenu::getId)
                .collect(Collectors.toList());

        // 获取用户的角色ID
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SysUserRole::getRoleId)
                .eq(SysUserRole::getUserId, userId);
        List<Long> roleIds = sysUserRoleMapper.selectList(queryWrapper)
                .parallelStream()
                .map(SysUserRole::getRoleId)
                .collect(Collectors.toList());


        // 根据菜单ids和角色ids查询页面元素信息
        Map<Long, List<SysElement>> elements = sysElementMapper.getSysElementsByRoleIdsAndMenuIds(roleIds, menuIds)
                .parallelStream()
                .collect(Collectors.groupingBy(SysElement::getMenuId));

        // 处理元素
        menus = menus.parallelStream()
                .map(menu -> menu.setElements(elements.get(menu.getId())))
                .collect(Collectors.toList());

        List<SysMenuVo> sysMenuVos = TreeUtil.builderAndConvert(menus, 0L, SysMenuVo.class, SysMenu.class);
        return sysMenuVos;

    }
}




